/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Utilities;
import util.*;
import model.ClientWorker;
import java.util.ArrayList;
import model.Message;

/**
 *
 * @author Asus
 */
public class Server {
    private ServerSocket mServer;
    private ArrayList<Socket> mClientSockets;
    String url;
    Connection con;
    
    public void listenSocket(){
        try{
            mServer = new ServerSocket(1235);
            url = "jdbc:mysql://localhost:3306/bropsdox";
            con = DriverManager.getConnection(url,"root", "");
        }catch(Exception ex){
            System.out.println("Could not listen on port 1235");
            System.exit(-1);
        }
        
        while(true){
            ClientWorker w;
            try{
                w = new ClientWorker(mServer.accept(), con);
                Thread t = new Thread(w);
                t.start();
            }catch(Exception ex){
                System.out.println("Accept failed: 1235");
                System.exit(-1);
            }
        }
    }
                
     public Server() throws IOException {
        mServer = new ServerSocket(util.Utilities.SERVER_PORT);
        mClientSockets = new ArrayList<Socket>();
    }
     
     public void start()  {
        System.out.println("Starting server.... \nListening to port " + util.Utilities.SERVER_PORT);
        while (true) {
            try {
                Socket clientSocket = mServer.accept();
                
                clientSocket.setSoTimeout(util.Utilities.SERVER_TIMEOUT);
                System.out.println("Accepting socket from " + clientSocket.getRemoteSocketAddress().toString());

                new ServerController(this, clientSocket).start();
                synchronized (this) {
                    mClientSockets.add(clientSocket);
                    ArrayList<Socket> deleted = new ArrayList<Socket>();
                    for (int i = 0; i < mClientSockets.size(); ++i) {
                        Socket socket = mClientSockets.get(i);

                        try {
                            OutputStream os = socket.getOutputStream();
                            // ngecek apakah socket nya masih idup pa ngga
                            os.write(Message.MSG_KEEP_ALIVE);
                            os.flush();
                        } catch (IOException iEx) {
                            System.out.println("Socket not connected");
                            socket.close();
                        }

                        if (socket.isClosed() || !socket.isConnected() || !socket.isBound())
                            deleted.add(mClientSockets.get(i));
                    }

                    for (int i = 0; i < deleted.size(); ++i)
                        mClientSockets.remove(deleted.get(i));
                }
                System.out.println("Handled socket = " + mClientSockets.size());
            } catch (IOException iEx) {
                System.out.println("Error is caused by " + iEx);
            }
        }
    }
     
     public static void main(String[] args) {
        try {
            Server server = new Server();

            server.start();
        } catch (IOException iEx) {
            System.out.println("Server fail! Caused by : " + iEx);
        }
    }
}
